package com.jingli.pile.converter;

import cn.hutool.core.util.RandomUtil;
import com.jingli.pile.entity.SupOperatorInfo;
import com.jingli.pile.entity.XtUser;
import com.jingli.pile.util.AuthUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

@Component
public class OperatorConvertor {
    private Logger logger = LoggerFactory.getLogger(OperatorConvertor.class);

    public XtUser convertSupOperatorToUser(SupOperatorInfo supOperatorInfo){
        XtUser user = new XtUser();
        user.setUseraccount(supOperatorInfo.getOperatorTel1());
        user.setRealName(supOperatorInfo.getOperatorName());
        user.setRoyaltyPercent(BigDecimal.ZERO);
        user.setIsOperator(1);
        user.setOperatorucsid(supOperatorInfo.getOperatorUcsId());
        user.setCreatetime(new Date());
        user.setIsvalid("Y");
        user.setDepartid("1001");
        user.setUserorder(0);
        user.setState(1);
        user.setAddress(supOperatorInfo.getOperatorRegAddress());
        user.setDataPermissions(1);
        user.setDeviceType(4);
        user.setIsShareholder(0);
        String salt = AuthUtils.getRandomString(AuthUtils.SALT_LENGTH);
        /*默认密码admin123456*/
        String password = AuthUtils.md5Hex("admin123456", salt);
        user.setSalt(salt);
        user.setPassword(password);
        return user;
    }

    public SupOperatorInfo convertUserTOOperator(XtUser xtUser){
        SupOperatorInfo supOperatorInfo = new SupOperatorInfo();
        supOperatorInfo.setOperatorId(RandomUtil.randomString(9));
        supOperatorInfo.setOperatorUcsId(xtUser.getOperatorucsid());
        supOperatorInfo.setOperatorName(xtUser.getRealName());
        supOperatorInfo.setUserId(Long.valueOf(xtUser.getId()));
        supOperatorInfo.setIsPush(1);
        supOperatorInfo.setCreateTime(new Date());
        supOperatorInfo.setOperatorRegAddress(xtUser.getAddress());
        supOperatorInfo.setOperatorTel1(xtUser.getMobile());
        return supOperatorInfo;
    }

}
